package database;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.*;

public class ShoppingDAO
{
	public static void create(Shopping shopping)
	{
		Conexao.connect();
		Conexao.update("INSERT INTO shopping (cnpj,nome,cidade,endereco,estado) VALUES (" + shopping.getCnpj() + ",'" +
		shopping.getNome() + "','" + shopping.getCidade() + "','" + shopping.getEndereco() + "','" +
		shopping.getEstado() + "');");
		Conexao.close();
	}

	public static Shopping read(String cnpj)
	{
		Shopping shopping = null;
		ResultSet resultSet = null;
		Conexao.connect();
		resultSet = Conexao.query("SELECT * FROM shopping WHERE cnpj ='" + cnpj +"';");
		try{
			if(resultSet.next())
			{
				shopping = new Shopping();
				shopping.setCnpj(cnpj);
				shopping.setNome(resultSet.getString("nome"));
				shopping.setCidade(resultSet.getString("cidade"));
				shopping.setEndereco(resultSet.getString("endereco"));
				shopping.setEstado(resultSet.getString("estado"));
			}
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
		Conexao.close();
		return shopping;
	}

	public static void update(Shopping shopping)
	{
		Conexao.connect();
		Conexao.update("UPDATE shopping SET nome='" + shopping.getNome() + "',cidade='" + shopping.getCidade() +
				"',endereco='" + shopping.getEndereco() + "',estado='" + shopping.getEstado() + 
				"' WHERE cnpj='" + shopping.getCnpj() + "';");
		System.out.println("UPDATE shopping SET nome='" + shopping.getNome() + "',cidade='" + shopping.getCidade() +
				"',endereco='" + shopping.getEndereco() + "',estado='" + shopping.getEstado() + 
				"' WHERE cnpj='" + shopping.getCnpj() + "';");
		Conexao.close();
	}

	public static void delete(String cnpj)
	{
		Conexao.connect();
		Conexao.update("DELETE FROM shopping WHERE cnpj='" + cnpj + "';");
		Conexao.close();
	}
	
	public static List<Shopping> getAll() throws SQLException {
		List<Shopping> shoppings = new ArrayList<Shopping>();
		String sqlClause = "SELECT * FROM shopping";
		ResultSet resultSet = null;
		
		Conexao.connect();
		resultSet = Conexao.query(sqlClause);
		while(resultSet.next()) {
			Shopping shopping = new Shopping();
			shopping.setCnpj(resultSet.getString("cnpj"));
			shopping.setNome(resultSet.getString("nome"));
			shopping.setEstado(resultSet.getString("estado"));
			shopping.setCidade(resultSet.getString("cidade"));
			shopping.setEndereco(resultSet.getString("endereco"));
			
			shoppings.add(shopping);
		}
		Conexao.close();
		return shoppings;
		
	}
}